<template>
  <div>
    <el-dialog title="编辑语音包" :visible.sync="dialogVisible" width="500px" :before-close="closeEditDialog"
      :close-on-click-modal="false" :append-to-body="true">
      <el-form :model="editData" :rules="rules" ref="refAddInfo" label-width="100px">

        <el-form-item size="small" label="租户名称" prop="tenantId" v-if="user.roleName=='PLATFORM_ADMIN'">
          <el-input v-model="editData.tenantName" disabled></el-input>
        </el-form-item>

        <!-- <el-form-item size="small" label="单位名称" prop="orgId" v-if="user.roleName=='TENANT_ADMIN'">
          <el-input v-model="editData.orgName" disabled></el-input>
        </el-form-item> -->

        <el-form-item size="small" label="总条数" prop="amount">
          <el-input clearable oninput="if(isNaN(value)) { value = null }"
            placeholder="请输入" v-model="editData.amount" maxlength="15"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeEditDialog" class="cancel-button">取 消</el-button>
        <el-button size="small" @click="editSubmit" v-preventReClick="1500" class="confirm-button">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqVoicePackageUpdate,
} from "@/api/api";
import treeselect from "@riophae/vue-treeselect"; //树形选择器
import "@components/vue-treeselect.less"; //树形选择器
import { mapState } from "vuex";

export default {
  name: "add",
  props: ["editData"],
  components: { treeselect },
  data() {
    return {
      dialogVisible: false,
      // 表单验证规则
      rules: {
        tenantId: [
          {
            required: true,
            message: "请选择租户名称",
            trigger: ["blur", "change"],
          },
        ],
        orgId: [
          { required: true, message: "请选择单位名称", trigger: "change" },
        ],
        amount: [{ required: true, message: "请输入总条数", trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    editSubmit() {
      this.$refs.refAddInfo.validate(async (valid) => {
        if (valid) {
          if(this.editData.amount==0) return this.$modal.notifyWarning('总条数不能为0');
          const res = await reqVoicePackageUpdate({
            id: this.editData.id,
            roleName: this.user.roleName,
            amount: this.editData.amount,
          });
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
          this.$modal.notifySuccess("编辑语音包成功");
          this.closeEditDialog();
          this.$parent.PageMessagePackage();
          if (this.user.roleName == "TENANT_ADMIN") {
            this.$parent.MessagePackage(); //更新统计
          }
        }
      });
    },
    // 点击关闭弹框
    closeEditDialog() {
      this.$refs.refAddInfo.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../../../common.less");
::v-deep .el-input.is-disabled .el-input__inner{
  border-color: @primaryBorderColor;
}
</style>